package cn.wizzer.app.web.modules.controllers.platform.user;


import cn.wizzer.app.user.modules.models.vo.LiveInfo;
import cn.wizzer.app.user.modules.services.LiveBroadcastService;
import cn.wizzer.framework.base.Result;
import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

@IocBean
@At("/platform/user/live")
public class LiveBroadcastController {

    @Inject
    @Reference
    private LiveBroadcastService liveBroadcastService;

    @At("/")
    @Ok("beetl:/platform/user/live/index.html")
    @RequiresPermissions("user.manager.live.list")
    public void index() {
    }

    /**
     * 获取直播列表
     *
     * @param code String 频道类型
     * @param pageNumber   int
     * @param pageSize     int
     * @return Object
     */
    @At
    @Ok("json:full")
    @RequiresPermissions("user.manager.live.list")
    public Object data(@Param("code") String code, @Param("searchKeyword") String searchKeyword, @Param("pageNumber") int pageNumber, @Param("pageSize") int pageSize) {
        try {
            return Result.success().addData(liveBroadcastService.getLiveList(code, searchKeyword, pageNumber, pageSize));
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }


    /**
     * 关闭直播
     *
     * @return Object
     */
    @At("/close/?")
    @Ok("json")
    @RequiresPermissions("user.manager.live.edit")
    public Object close(String appid) {
        return liveBroadcastService.end(appid);
    }
    /**
     * 关闭直播
     *
     * @return Object
     */
    @At("/closeByNo/?")
    @Ok("json")
    @RequiresPermissions("user.manager.live.edit")
    public Object closeByNo(String id) {
        return liveBroadcastService.removeLive(id);
    }

    @At("/edit/?")
    @Ok("json")
    public Object edit(@Param("id") String id) {
        return Result.success(liveBroadcastService.hget(id));
    }

    @At("/editDo")
    @Ok("json")
    public Object editDo(@Param("..") LiveInfo liveInfo) {
        if(Strings.isBlank(liveInfo.getMatchId())){
            return Result.error("请选择赛事");
        }
        //设置直播数据标签
        liveBroadcastService.updateLabelsByLiveNo(liveInfo.getId(), liveInfo.getLabels(), liveInfo.getMatchId(), false);
        return Result.success();
    }
}
